<template>
  <div id="contant">
    <!-- <h3><dv-decoration-7 class="baogao">学情报告</dv-decoration-7></h3> -->
    <div class="DataShow">
      <div class="block first">
        <div class="tu">
          <img class="tu" src="@/assets/personal/chengji.png" alt="" />
        </div>
        <div class="main">
          <p>班级平均成绩排名</p>
          <p>
            <span>{{ ranking }} 名</span>
          </p>
        </div>
      </div>
      <div class="block second">
        <div class="tu">
          <img class="tu" src="@/assets/personal/tushu.png" alt="" />
        </div>
        <div class="main">
          <p>图书借阅情况</p>
          <p>
            <span>{{ bookNum }}本/月</span>
          </p>
        </div>
      </div>
      <div class="block third">
        <div class="tu">
          <img class="tu" src="@/assets/personal/tice.png" alt="" />
        </div>
        <div class="main">
          <p>体测成绩状态</p>
          <p>
            <span>{{ status }}</span>
          </p>
        </div>
      </div>
      <div class="block fourth">
        <div class="tu">
          <img class="tu" src="@/assets/personal/wenjian.png" alt="" />
        </div>
        <div class="main">
          <p>平均绩点</p>
          <p>
            <span>{{ gpa }}</span>
          </p>
          <a class="make" href="#" @click="generateReport">生成报告</a>
        </div>
        <template>
          <div v-if="loading" class="loading-overlay">
            <div class="loading-spinner"></div>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import pubsub from "pubsub-js";
export default {
  name: "HorizontalLayout",
  data() {
    return {
      stuId: "",
      loading: false,
      ranking: 0, //排名
      bookNum: 0,
      gpa: 0,
      status: "",
      possibleStatus: ["良好", "优秀", "一般"],
      possibleGPAs: [4.0, 3.7, 3.3, 3.0, 2.7],
    };
  },
  mounted() {
    this.pubId = pubsub.subscribe("selectStuInfo", (msgName, data) => {
      this.stuId = data;
      this.randomNumber();
    });
    this.interval = window.setInterval(() => {
      this.bookNum += 1;
    }, 10000);
  },
  methods: {
    generateReport() {
      // this.showLoading();
      let loadingInstance = this.$loading({
        lock: false,
        text: "正在生成报告......",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.3)",
      });
      this.$http({
        url: this.$http.adornUrl(`/chaoxing/push/push`),
        method: "post",
      }).then((response) => {});

      this.$http({
        url: this.$http.adornUrl(`/chaoxing/push/push/${this.stuId}`),
        method: "post",
      }).then((response) => {
        // console.log(response);
        // console.log(response.data);
        if (response.data.code == 0) {
          // this.hideLoading();
          loadingInstance.close();
          this.$message.success("成功生成,请查收!");
        }
      });
    },
    randomNumber() {
      this.ranking = Math.floor(Math.random() * (30 - 1 + 1)) + 1;
      this.bookNum = Math.floor(Math.random() * (20 - 1 + 1)) + 5;
      let randomIndex1 = Math.floor(Math.random() * 5);
      this.gpa = this.possibleGPAs[randomIndex1];
      let randomIndex2 = Math.floor(Math.random() * 3);
      this.status = this.possibleStatus[randomIndex2];
    },
    showLoading() {
      this.loading = true;
    },
    hideLoading() {
      this.loading = false;
    },
  },
  beforeDestroy() {
    pubsub.unsubscribe(this.pubId);
    window.clearInterval(this.interval); // 一定要清除定时器
  },
};
</script>

<style scoped>
#contant {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}
#contant h3,
.baogao {
  flex: 1;
  text-align: left;
  padding: 5px 0 5px 20px;
}
.baogao {
  flex: 1;
  text-align: left;
  /* background-color: #d1b1f3; */
  padding: 0;
}
.DataShow {
  flex: 9;
  display: flex;
  width: 100%;
  height: 100%;
}

.block {
  flex: 1;
  text-align: center;
  padding: 10px;

  display: flex;
  /* flex-direction: column; */
  box-sizing: border-box;
  margin: 10px;
}
.tu,
.main {
  box-sizing: border-box;
}
.tu {
  flex: 1;
  padding: 5px;

  box-sizing: border-box;
}
.main {
  flex: 1.5;
  padding: 10px;
  box-sizing: border-box;
  /* background-color: #e8f3b1; */

  position: relative;
}
.main p {
  font-size: 16px;
  text-align: left;
  margin-left: 5px;
  line-height: 30px;
}
.main span {
  color: #4bcffc;
  font-size: 35px;
  box-sizing: border-box;
  /* margin-left: 40px;
    margin-right: 5px; */
  /* margin: 80px 5px -10px 40px; */
  /* line-height:20px; */
  position: absolute;
  bottom: 25px;
  left: 35px;
}
.make {
  color: #4bcffc;
  text-shadow: 5px 3px 3px;
  font-size: 15px;
  margin-left: 70px;
  cursor: pointer;
  /* line-height:10px */
  position: absolute;
  bottom: 0px;
  right: 10px;
}
.first {
  /* background-color: #f3b1b1; */
  margin-left: 20px;
}
/* .second{
    background-color: #d1b1f3;
  }
  .third{
    background-color: #b1f3d4;
  } */
.fourth {
  /* background-color: #e8f3b1; */
  margin-right: 20px;
}

.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-spinner {
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top: 4px solid #ffffff;
  width: 50px;
  height: 50px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>
